package pack0826;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DemoA {
    //List---->ArrayList
    public static void a(){
        //新建一个空的集合
        List<String> list = new ArrayList<String>();
        //添加
        list.add("西安");
        list.add("宝鸡");
        list.add("上海");
        list.add("武汉");
        list.add("成都");
        list.add("福州");
        list.add("福州");
        //插入添加（插入到第三个前面）
        list.add(2,"海上");
        System.out.println(list);
        //删除
        list.remove(4);//删除指定位置，不可以越界
        list.remove("上海");
        list.remove("北京");//可以删除集合里面没有的元素
        list.remove("福州");//有重复的删除第一个元素
        System.out.println(list);
        //获取集合中指定位置的元素
        String s = list.get(3);
        System.out.println(s);
        //获取集合的长度
        int size = list.size();
        System.out.println(size);
        //清空集合
        list.clear();
        System.out.println(list);
    }
    //判断集合中的元素和位置
    public static void b1(){
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i<10; i++){
            list.add((int)(Math.random()*10));
        }
        System.out.println(list);
        boolean b1 = list.contains(7);//判断是否有“7”
        int index1 = list.indexOf(0);//判断第一个“0”在哪个位置
        int index2 = list.lastIndexOf(0);//判断最后一个“0”在哪个位置
        System.out.println(b1);
        System.out.println(index1);
        System.out.println(index2);
    }

    public static void b2(){
        List list = new ArrayList<>();
        Users u1 = new Users(1,"张三",125.3);
        Users u2 = new Users(2,"李四",145.2);
        Users u3 = new Users(3,"王五",89.8);
        list.add(u1);
        list.add(u2);
        list.add(u3);
        Users u4 = new Users(3,"王五",89.8);
        boolean b = list.contains(u4);
        int index = list.indexOf(u4);
        System.out.println(index);
        System.out.println(b);
        System.out.println(u3==u4);
    }

    public static void b3(){
        Users u1 = new Users(1,"aa",25.2);
        Users u2 = new Users(2,"aa",25.2);
        System.out.println(u1==u2);
        System.out.println(u1.equals(u2));
    }
    //对集合中的元素排序
    public static void c1(){
        List<Integer> list = new ArrayList<>();
        list.add(87);
        list.add(98);
        list.add(54);
        list.add(87);
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }

    public static void c2(){
        List list = new ArrayList<>();
        Users u1 = new Users(1,"张三",125.3);
        Users u2 = new Users(2,"李四",145.2);
        Users u3 = new Users(3,"王五",89.8);
        list.add(u1);
        list.add(u2);
        list.add(u3);
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }

    public static void c3(){
        Users u1 = new Users(1,"张三",23.23);
        System.out.println(u1);
    }


    public static void main(String[] args) {
        c3();
    }
}
